unit currency_form;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ImgList,  m_api, m_globaldefs, dbsettings, StrUtils, cx_procs, cx_vars, cx_google, cx_contacts;

type
  TXCHForm = class(TForm)
    Memo1: TMemo;
    Button2: TButton;
    ImageList1: TImageList;
    Button5: TButton;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    list: TListView;
    cbIcon: TComboBoxEx;
    edTo: TEdit;
    cbTo: TComboBox;
    cbFrom: TComboBox;
    cbSource: TComboBox;
    Button1: TButton;
    edCaption: TEdit;
    Label1: TLabel;
    edStatusMsg: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    edHistoryLog: TEdit;
    GroupBox1: TGroupBox;
    ListView1: TListView;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    exampleCC: TLabel;
    exampleSM: TLabel;
    exampleHL: TLabel;
    Button3: TButton;
    Button4: TButton;
    TabSheet3: TTabSheet;
    edInterval: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    edPrecision: TEdit;
    Label10: TLabel;
    Button6: TButton;
    procedure edFromKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure RedrawEdits;
    procedure listMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure listSelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure UpdateEdits(from: TListItem);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure LoadSettings;
    procedure WriteSettings;
    procedure RefreshRatesList;
    procedure Button5Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure edToChange(Sender: TObject);
    procedure edCaptionChange(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  XCHForm: TXCHForm;
  selectedContact : THandle;

implementation


{$R *.dfm}

Procedure TXCHForm.RedrawEdits;
begin
with list do
  begin
  cbIcon.Left:=Left+BorderWidth;
  edTo.Left:=cbIcon.Left+cbIcon.Width+BorderWidth;
  edTo.Width:=Column[0].Width-BorderWidth-cbTo.Width-cbIcon.Width;
  cbTo.Left:=edTo.Left+edTo.Width+BorderWidth;


  cbFrom.Left:=cbTo.Left+cbTo.Width+BorderWidth;
  cbFrom.Width:=Column[1].Width+BorderWidth;


  cbSource.Left:=cbFrom.Left+cbFrom.Width+BorderWidth;
  cbSource.Width:=Column[2].Width-BorderWidth;

  end;
end;

procedure TXCHForm.edFromKeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in [#8, '0'..'9', DecimalSeparator]) then begin
    Key := #0;
  end
  else if (Key = DecimalSeparator) and
          (Pos(Key, (Sender as TEdit).Text) > 0) then begin
    Key := #0;
  end;

end;

procedure TXCHForm.FormShow(Sender: TObject);
begin
  DecimalSeparator:='.';
  RedrawEdits;
  LoadRates;
  RefreshRatesList;
  LoadSettings;
  edCaptionChange(nil);  
end;


procedure TXCHForm.listMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  RedrawEdits;
end;

procedure TXCHForm.Button1Click(Sender: TObject);
var
  hContactNew:THandle;
  rateto,ratefrom,ratesource : string;
  contact_id  : THandle;
  Amount  : integer;

begin
rateto:=edto.Text+' '+cbTo.Text;
list.AddItem(rateto,nil);

with list.Items[list.Items.Count-1] do
begin
  SubItems.Add(cbFrom.Text);
  SubItems.Add('');
  SubItems.Add(cbSource.Text);
end;
  Amount:=strtoint(edTo.Text);
  list.Selected:=List.Items[list.Items.Count-1];
  contact_id:=cx_AddRate(StrToInt(AnsiReplaceStr(AnsiReplaceStr(TimeToStr(Time),'.',''),':','')),rateto,cbFrom.Text, cbSource.Text, cbTo.Text,Amount);
  list.Items[list.Items.Count-1].SubItems.Add(IntToStr(contact_id));
  {}

end;

procedure TXCHForm.listSelectItem(Sender: TObject; Item: TListItem;
  Selected: Boolean);
begin
  UpdateEdits(Item);
end;

procedure TXCHForm.UpdateEdits(from: TListItem);
var
src,qty,cur : string;
begin
  qty:=AnsiLeftStr(from.Caption,PosEx(from.SubItems[0],' ')+1);
  cur:=AnsiRightStr(from.Caption,PosEx(from.SubItems[0],' '));
  with list do
  begin
    cbTo.Text:=cur;
    edTo.Text:=qty;
    cbFrom.Text:=from.SubItems[0];
    cbSource.Text:=from.SubItems[1];
  end;
end;

procedure TXCHForm.RefreshRatesList;
var
  i : integer;
begin
  list.Clear;
for i:=0 to length(cx_CList)-1 do
  with list.Items.Add do
   begin
     Caption:=IntToStr(cx_CList[i].Amount)+' '+cx_CList[i].Currency;
     Subitems.Add(cx_CList[i].StateIn);
     Subitems.Add(cx_Clist[i].CurrentRate);
     Subitems.Add(cx_CList[i].Source);
     Subitems.Add(IntToStr(cx_CList[i].ID));
//     MessageBox(0,cx_CList[i].Name,'Test',MB_OK);
   end;
end;

procedure TXCHForm.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
response : string;
I: Integer;
begin



  LoadRates;
  RefreshRatesList

end;

procedure TXCHForm.Button5Click(Sender: TObject);
begin
memo1.Lines.Add(FloatToStr(Google_GetRate('JPY','RUR').current.rate));
end;


procedure TXCHForm.LoadSettings;
var
  Check : string;
begin
  try
   case DBGetSettingType(0, piShortName, cx_opt_OptsExist) of
    DBVT_DELETED: check := 'No Settings!';
    DBVT_ASCIIZ:
     try
      check := StrPas(DBReadString(0,piShortName,cx_opt_OptsExist));
     except
      ShowMessage('Wrong string reading...');  //debug
     end;
    else
      check:='';
    end;
   //ShowMessage(check); //debug
  except
   ShowMessage('Wrong type reading...');  //debug
  end;
  if check=cx_opt_OptsExist then
  begin
    edCaption.Text:=StrPas(DBReadString(0,piShortName,cx_opt_ContactCaption));
    edStatusMsg.Text:=StrPas(DBReadString(0,piShortName,cx_opt_StatusMessage));
    edHistoryLog.Text:=StrPas(DBReadString(0,piShortName,cx_opt_HistoryLog));
    edInterval.Text:=IntToStr(DBGetContactSettingDword(0,piShortName,cx_opt_RefreshInterval,0));
    edPrecision.Text:=IntToStr(DBGetContactSettingDword(0,piShortName,cx_opt_Precision,0));
  end
  else
    XCHForm.WriteSettings;

{}
end;

procedure TXCHForm.WriteSettings;
begin
  DBWriteContactSettingString(0,piShortName,cx_opt_OptsExist,cx_opt_OptsExist);
  DBWriteContactSettingString(0,piShortName,cx_opt_ContactCaption,PChar(edCaption.Text));
  DBWriteContactSettingString(0,piShortName,cx_opt_StatusMessage, PChar(edStatusMsg.Text));
  DBWriteContactSettingString(0,piShortName,cx_opt_HistoryLog, PChar(edStatusMsg.Text));
  DBWriteContactSettingDWord(0,piShortName,cx_opt_RefreshInterval, StrToIntDef(edInterval.Text, IntervalDefault));
  DBWriteContactSettingDWord(0,piShortName,cx_opt_Precision,StrToIntDef(edPrecision.Text, PrecisionDefault));
{}
end;

procedure TXCHForm.Button2Click(Sender: TObject);
begin
  XCHForm.WriteSettings;
  cx_RefreshContacts;
  XCHForm.Close;
  XCHForm.Free;
end;

procedure TXCHForm.edToChange(Sender: TObject);

begin
//    DBWriteContactSettingDWord(selectedContact, piShortName, cx_Amount, StrToInt(edTo.Text));
//    cx_UpdateContact(selectedContact);
end;

procedure TXCHForm.edCaptionChange(Sender: TObject);
begin

  exampleCC.Caption:=cx_FormatString(edCaption.Text);
  exampleSM.Caption:=cx_FormatString(edStatusMsg.Text);
  exampleHL.Caption:=cx_FormatString(edHistoryLog.Text);
  XCHForm.Refresh;

end;

procedure TXCHForm.Button4Click(Sender: TObject);
begin
  XCHForm.WriteSettings;
  cx_RefreshContacts;
end;

procedure TXCHForm.Button3Click(Sender: TObject);
begin
try
 XCHForm.Close;
// XCHForm.Free;
except
end;
end;

procedure TXCHForm.Button6Click(Sender: TObject);
begin
 If list.Selected = nil then exit;
 cx_DeleteRate(strtoint(list.Selected.SubItems[3]));
 //cx_RefreshContacts;
end;

end.
